✅ Overview:
- Similar to Python lists or NumPy arrays (dynamic, flexible).
- Elements stored in contiguous memory → efficient for
pushandpopat front/back. - Operations take constant time regardless of queue size.
Declaration & Basic Operations
int j = 1,
q2[$] = {3,4}, // Queue with 2 elements
q[$] = {0,2,3}; // Queue with 3 elements
initial begin
q.insert(1, j); // Insert j at index 1
q.delete(1); // Delete element at index 1
q.push_front(6); // Add element at front
j = q.pop_back(); // Remove last element, assign to j
q.push_back(8); // Add element at end
j = q.pop_front(); // Remove first element, assign to j
foreach (q[i])
$display(q[i]); // Iterate over queue
q.delete(); // Delete entire queue
end
Inserting queue inside a queue
int j=1,
q2[$] = {3,4},
q[$] = {0,2,5};
initial begin
q = {q[0], j, q[1:$]}; // Insert j after first element
q = {q[0:2], q2, q[3:$]}; // Insert q2 after index 2
q = {q[0], q[2:$]}; // Remove element at index 1
q = {6, q}; // Push at front
j = q[$]; // Access last element
q = q[0:$-1]; // Pop from back
q = {q, 8}; // Push at back
j = q[0]; // Access first element
q = q[1:$]; // Pop from front
q = {}; // Clear queue
end
Key Points:
q[$]→ last elementq[0]→ first elementq[1:$]→ slice from index 1 to endq.insert(index, value)/q.delete(index)→ random access insert/delete- Concatenation
{}allows queue merging or element insertion anywhere. push_*/pop_*→ efficient operations for front/back